﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
//using System.Guid;

namespace E01
{
    /// <summary>
    /// 同步客户
    /// </summary>
    public class A04 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A04P P = (E01A04P)handler.Serial.Deserialize(st, typeof(E01A04P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[9];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;
                sps[3] = new SqlParameter("@ShortNumber", SqlDbType.NVarChar, 50);
                sps[3].Value = P.ShortNumber;
                sps[4] = new SqlParameter("@ValueAddRate", SqlDbType.Decimal);
                sps[4].Value = P.ValueAddRate;
                sps[5] = new SqlParameter("@FullName", SqlDbType.NVarChar, 50);
                sps[5].Value = P.FullName;
                sps[6] = new SqlParameter("@ItemClassID", SqlDbType.Int);
                sps[6].Value = P.ItemClassID;
                sps[7] = new SqlParameter("@UseSign", SqlDbType.Int);
                sps[7].Value = P.UseSign;
                sps[8] = new SqlParameter("@Guid", SqlDbType.UniqueIdentifier);
                sps[8].Value = Guid.NewGuid();
                
                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @t_OrgFID INT\r\n SET @t_OrgFID=IsNull((SELECT FItemID FROM t_Organization WHERE FHomePage=@ID AND FHomePage<>''),0)\r\n");
                sql.Append("DECLARE @t_ItemCount INT\r\n SET @t_ItemCount=IsNull((SELECT Count(1) FROM t_Item WHERE FNumber=@Number ),0)\r\n");
                sql.Append("IF @t_OrgFID>0  BEGIN\r\n");
                sql.Append("  UPDATE t_Organization SET FName=@Name,FValueAddRate=@ValueAddRate WHERE FItemID=@t_OrgFID\r\n");
                sql.Append("  UPDATE t_Item SET FName=@Name, FUseSign=@UseSign WHERE FItemID=@t_OrgFID\r\n");
                sql.Append("  SELECT 'U',@t_OrgFID\r\n");
                sql.Append("END\r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 't_Item',@t_OrgFID OUTPUT\r\n");
                sql.Append("  INSERT INTO t_Organization ([FClassTypeID],[FItemID],[FStatus],[FRegionID],[FTrade],[FCyID],[FSetID],[FDeleted],[FSaleMode],[FNumber],[FName],[FParentID] ,[FShortNumber],[FARAccountID],[FAPAccountID],[FpreAcctID],[FlastTradeAmount],[FlastRPAmount],[Fdepartment],[Femployee],[FcashDiscount],[FcurrencyID],[FmaxDealAmount],[FminForeReceiveRate],[FminReserverate],[FdebtLevel],[FCarryingAOS],[FIsCreditMgr],[FCreditPeriod],[FCreditLevel],[FPayTaxAcctID],[FValueAddRate],[FTypeID],[FStockIDAssign],[FStockIDInst],[FStockIDKeep],[FLicAndPermit],[FOtherARAcctID],[FOtherAPAcctID],[FPreAPAcctID],[FSaleID],[FRegion],[FPayCondition],[FManageType],[FClass],[FValue],[FRegUserID],[FFlat],[FCoSupplierID],[FShareStatus],[FHomePage]) ");
                sql.Append("  VALUES (1012000,@t_OrgFID,1072,0,0,0,0,0,1057,@Number,@Name,0,@ShortNumber,0,0,0,0.00,0.00,0,0,'',0,0,1,1,0,0,0,0,0,0,@ValueAddRate,0,0,0,0,1,0,0,0,0,0,0,0,0,'',0,1,0,0,@ID)\r\n");
                sql.Append("  INSERT INTO t_Item ([FItemClassID],[FItemID],[FExternID],[FNumber],[FParentID],[FLevel],[FDetail],[FName],[FUnUsed],[FBrNo],[FFullNumber],[FDiff],[FDeleted],[FShortNumber],[FFullName],[UUID],[FGRCommonID],[FSystemType],[FUseSign],[FAccessory],[FGrControl],[FHavePicture]) ");
                sql.Append("  VALUES ( @ItemClassID ,@t_OrgFID,-1,@Number,0,4,1,@Name,0,0,@Number,0,0,@ShortNumber,@FullName,@Guid,-1,1,@UseSign,0,-1,0)\r\n");
                sql.Append("  SELECT 'I',@t_OrgFID\r\n");
                sql.Append("  END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("客户同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("客户同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("客户同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 客户
    /// </summary>
    public class E01A04P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 客户简码
        /// </summary>
        public String ShortNumber;
        /// <summary>
        /// 增值税率
        /// </summary>
        public Decimal ValueAddRate;
        /// <summary>
        /// 销售模式
        /// </summary>
        //public String FSaleMode;
        /// <summary>
        /// 全名
        /// </summary>
        public String FullName;
        /// <summary>
        /// 客户类别 1客户 4供应商
        /// </summary>
        public int ItemClassID;
        /// <summary>
        /// 所处层级
        /// </summary>
        //public int Level;
        /// <summary>
        /// 是否最下层级
        /// </summary>
        //public Boolean Detail;
        /// <summary>
        /// 使用标记
        /// </summary>
        public int UseSign;
        
    }
}
